<template>
  <div>
    <el-card class="box-card" style="margin:10px 0">
      <div slot="header" class="clearfix">
        <el-tabs v-model="activeName" class="tab">
          <el-tab-pane label="销售额" name="sale"></el-tab-pane>
          <el-tab-pane label="访问量" name="visit"></el-tab-pane>
        </el-tabs>
        <div class="right">
          <span @click="setDay">今日</span>
          <span @click="setWeek">本周</span>
          <span @click="setMonth">本月</span>
          <span @click="setYear">本年</span>
          <el-date-picker
            v-model="date"
            class="date"
            type="daterange"
            size="mini"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          />
        </div>

      </div>
      <div>
        <el-row :gutter="10">
          <el-col :span="18">
            <!--容器 用于放置柱状图-->
            <div ref="charts" class="charts"/>
          </el-col>
          <el-col :span="6">
            <!-- 门店销售额排名 -->
            <div class="right">
              <h3>门店{{ title }}排名</h3>
              <ul>
                <li><span class="index">1</span><span>肯德基</span><span class="number">123456</span></li>
                <li><span class="index">2</span><span>肯德基</span><span class="number">123456</span></li>
                <li><span class="index">3</span><span>肯德基</span><span class="number">123456</span></li>
                <li><span class="index">4</span><span>肯德基</span><span class="number">123456</span></li>
                <li><span class="index">5</span><span>肯德基</span><span class="number">123456</span></li>
                <li><span class="index">6</span><span>肯德基</span><span class="number">123456</span></li>
                <li><span class="index">7</span><span>肯德基</span><span class="number">123456</span></li>
                <li><span class="index">8</span><span>肯德基</span><span class="number">123456</span></li>
                <li><span class="index">9</span><span>肯德基</span><span class="number">123456</span></li>

              </ul>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import dayjs from 'dayjs'

export default {
  name: 'Sale',
  data() {
    return {
      activeName: 'sale',
      myEcharts: null,
      // 收集日历的数据
      date: ''
    }
  },
  computed: {
    // 计算属性--标题
    title() {
      return this.activeName === 'sale' ? '销售额' : '访问量'
    }
  },
  mounted() {
    this.myEcharts = echarts.init(this.$refs.charts)
    this.myEcharts.setOption({
      title: {
        text: this.title + '趋势'
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        }
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: [
        {
          type: 'category',
          data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
          axisTick: {
            alignWithLabel: true
          }
        }
      ],
      yAxis: [
        {
          type: 'value'
        }
      ],
      series: [
        {
          name: 'Direct',
          type: 'bar',
          barWidth: '40%',
          data: [20, 14, 73, 41, 78, 123, 73, 112, 199, 24, 62, 185]
        }
      ]
    })
  },
  watch: {
    // 监听
    title() {
      // 重新修改图表的配置数据
      this.myEcharts.setOption({
        title: {
          text: this.title + '趋势'
        }
      })
    }
  },
  methods: {
    // 本天
    setDay() {
      const day = dayjs().format('YYYY-MM-DD')
      // console.log(day)
      this.date = [day, day]
    },
    // 本周
    setWeek() {
      const start = dayjs().day(1).format('YYYY-MM-DD')
      const end = dayjs().day(7).format('YYYY-MM-DD')
      // console.log(start, end)
      this.date = [start, end]
    },
    // 本月
    setMonth() {
      const start = dayjs().startOf('month').format('YYYY-MM-DD')
      const end = dayjs().endOf('month').format('YYYY-MM-DD')
      // console.log(start, end)
      this.date = [start, end]
    },
    // 本年
    setYear() {
      const start = dayjs().startOf('year').format('YYYY-MM-DD')
      const end = dayjs().endOf('year').format('YYYY-MM-DD')
      this.date = [start, end]
    }

  }
}
</script>

<style scoped>
.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;
}

.tab {
  width: 100%;
}

.clearfix .right {
  /*子绝父相*/
  position: absolute;
  right: 0;
}

.date {
  width: 250px;
  margin: 0px 10px;
}

.right span {
  font-size: 14px;
  margin: 0 10px;
}

.charts {
  width: 100%;
  height: 300px;
}

ul {
  list-style: none;
  width: 100%;
  height: 300px;
  padding: 0;
}

ul li {
  height: 8%;
  padding: 0;
}

.index {
  float: left;
  width: 20px;
  height: 20px;
  background: #000;
  color: #ffffff;
  text-align: center;
  border-radius: 50%;
  padding: 0;
  margin: 0px;
}

</style>
